package comydlclass;

import com.ydlclass.exceptions.ZookeeperException;
import com.ydlclass.utils.zookeeper.ZookeeperNode;
import com.ydlclass.utils.zookeeper.ZookeeperUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.*;

import java.util.List;
import java.util.function.Consumer;

/**
 * 注册中心的管理页面
 * @author Knife
 */
@Slf4j
public class Application {
    public static void main(String[] args) {
        // 帮我们创建基础目录
        // yrpc-metadata   (持久节点)
        //  └─ providers （持久节点）
        //  		└─ service1  （持久节点，接口的全限定名）
        //  		    ├─ node1 [data]     /ip:port
        //  		    ├─ node2 [data]
        //            └─ node3 [data]
        //  └─ consumers
        //        └─ service1
        //             ├─ node1 [data]
        //             ├─ node2 [data]
        //             └─ node3 [data]
        //  └─ config


            // 创建一个zookeeper实例
            ZooKeeper zooKeeper = ZookeeperUtil.creatZookeeper();
            // 定义节点和数据
            String basePath = "/yrpc-metadata";
            String providersPath = basePath + "/providers";
            String consumersPath = basePath + "/consumers";

            ZookeeperNode baseNode = new ZookeeperNode(basePath, null);
            ZookeeperNode providersNode = new ZookeeperNode(providersPath, null);
            ZookeeperNode consumersNode = new ZookeeperNode(consumersPath, null);

            List.of(baseNode, providersNode, consumersNode).forEach(zookeeperNode -> {
                ZookeeperUtil.creatNode(zooKeeper,zookeeperNode,null,CreateMode.PERSISTENT);
            });

            // 关闭连接
            ZookeeperUtil.close(zooKeeper);

    }
}
